<?php
namespace app\index\controller;

use think\Db;
use think\Controller;
class Index extends Controller
{

      public function index(){
            $first_num = input('get.first_num/d',0,'int');
            $num = input('get.num/d',10,'int');

            $con=array();
            $con['id']=['gt',0];
            $con['date']=['like',date('Y-m-d',time())."%"];
            $list = Db::name('caoliujishu')->field('id')->where($con)->order("date desc")->select();
            $id_arr = array_column($list, 'id');
       
            $total = Db::name('caoliujishu')->where($con)->count(1);

            $con=array();
            $con['id']=['in',$id_arr];
            $r_list = Db::name('caoliujishu')->field('id,title,date')->where($con)->order("date desc")->select();

            $this->assign('list',$r_list);
            return $this->fetch();  
      }


      public function get_content(){
            $id = input('post.id/d',0,'int');

            if(!is_numeric($id) || $id<=0){
                  return ['data'=>'','errcode'=>'1001','errmsg'=>'非法错误'];
            }
            $con=array();
            $con['id']=$id;
            $info = Db::name('caoliujishu')->field('content,title,url')->where($con)->find();
            // $info['date']=substr($info['date'], 0,-3);
            $info['content'] = preg_replace('#<img(.+?)data-src="([^"]+?)"([^>]*?)>#',"<img$1src=\"$2\"$3>",$info['content']);
            return ['data'=>$info,'errcode'=>0,'errmsg'=>'完成'];
      }


      public function upload(){

            $file = request()->file('image');

            if($file){
                  $info = $file->validate(['size'=>156780,'ext'=>'jpg,jpeg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
                  if($info){ 
                        return json(['code'=>1,'imgurl'=>$info->getSaveName()]);
                  }else{
                        return json(['code'=>0,'msg'=>$info->getError()]);
                  }
            }
      }


}	